//引入express模块
const express=require('express');
//引入连接池对象
const pool=require('../pool.js');
//console.log(pool);
//const app=express();
//创建路由器对象
const r=express.Router();
// 油价接口（get  /familyson）
r.get('',(req,res,next)=>{
    const p=req.query.pname
    // const t=req.query.updatetime
    console.log(p);
    // pool.query('SELECT CAST(oil_92 AS CHAR) oil_92,pname FROM oil_family,area_list where oid=pid and pid=? and updatetime=?',[p,t],(err,result)=>{
    pool.query('SELECT * FROM oil_family,area_list where oid=pid and pname=? order by updatetime desc limit 11',[p],(err,result)=>{
        if(err){
            next(err);
            return;
        }else{
        console.log(result);
    }
        res.send(result);
    })
})
// 新增
r.post('/familyson',(req,res,next)=>{
    pool.query('insert into oil_family set ?',[req.body],(err,result)=>{
        if(err){
            throw err;
        }
        console.log(result);
    })
    res.send({code:202,msg:'油价数据添加成功'})
})
// 删除
r.delete('/:fid',(req,res,next)=>{
    pool.query('delete from oil_family where fid=?',[req.params.fid],(err,result)=>{
        if(err){
            next (err);
            return
        }
        console.log(result);
    })
    res.send({code:203,msg:'油价数据删除成功'})
})



//暴露(导出)路由器对象
module.exports=r;